package cn.lg.soar.system.auth.service;

import cn.lg.soar.common.util.current.IBaseUser;
import cn.lg.soar.system.auth.entity.TemporaryPassword;
import cn.lg.soar.system.auth.model.TemporaryPasswordVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * TemporaryPassword服务层接口
 * @author luguoxiang
 * @since 2022-04-30
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public interface ITemporaryPasswordService extends IService<TemporaryPassword> {

    /**
     * 创建临时密码
     * @param user
     * @param vo
     */
    Long create(IBaseUser user, TemporaryPasswordVo vo);

    /**
     * 更新
     * @param user
     * @param vo
     */
    void update(IBaseUser user, TemporaryPasswordVo vo);

    /**
     * 根据用户id获取
     * @param userId
     * @return
     */
    default TemporaryPassword getByUserId(Long userId) {
        return getOne(
                Wrappers.<TemporaryPassword>lambdaQuery().eq(TemporaryPassword::getUserId, userId)
        );
    }

    /**
     * 根据用户id获取
     * @param userId
     * @return
     */
    default List<TemporaryPassword> listByUserId(Long userId) {
        return list(
                Wrappers.<TemporaryPassword>lambdaQuery().eq(TemporaryPassword::getUserId, userId)
        );
    }

    /**
     * 登录计数
     * @param data
     * @return
     */
    default boolean loginCount(TemporaryPassword data) {
        return update(
                Wrappers.<TemporaryPassword>lambdaUpdate()
                        .set(TemporaryPassword::getLastLoginDate, LocalDateTime.now())
                        .set(TemporaryPassword::getUsageCount, data.getUsageCount() + 1)
                        .eq(TemporaryPassword::getId, data.getId())
        );
    }

    /**
     * 验证临时密码
     * @param id
     * @param username
     * @param password
     * @return
     */
    boolean verifyPassword(Long userId, String username, String password);

}
